﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class ChangeACL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newClassObject = from s in db.ClassInfo
                                         select new { ObjectID = s.ClassID, ObjectName = s.ClassName, ObjectType = "班级" };
                    var newCourseObject = from s in db.CourseInfo
                                         select new { ObjectID = s.CourseID, ObjectName = s.CourseName, ObjectType = "课程" };
                    var newObject = newClassObject.Concat(newCourseObject);
                    Store3.DataSource = newObject;
                    Store3.DataBind();
                    Store5.DataSource = newObject;
                    Store5.DataBind();
                }
                UserDataBind();
                RoleDataBind();
                MenuDataBind();
            }
        }

        protected void UserDataBind()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newClassACL = from s in db.ACL
                                  join t in db.ClassInfo on s.ObjectID equals t.ClassID
                                  where s.ObjectType.Trim() == "class" && s.SubjectType.Trim() == "user" && s.ACLValue / 16 >= 1
                                  select new { ObjectName = t.ClassName, SubjectName = s.SubjectName, ObjectType = "班级", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "强", AclID = s.AclID };
                var newClassACL2 = from s in db.ACL
                                  join t in db.ClassInfo on s.ObjectID equals t.ClassID
                                  where s.ObjectType.Trim() == "class" && s.SubjectType.Trim() == "user" && s.ACLValue / 16 < 1
                                  select new { ObjectName = t.ClassName, SubjectName = s.SubjectName, ObjectType = "班级", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "弱", AclID = s.AclID };
                var newCourseACL = from s in db.ACL
                                   join t in db.CourseInfo on s.ObjectID equals t.CourseID
                                   where s.ObjectType.Trim() == "course" && s.SubjectType.Trim() == "user" && s.ACLValue / 16 >= 1
                                   select new { ObjectName = t.CourseName, SubjectName = s.SubjectName, ObjectType = "课程", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "强", AclID = s.AclID };
                var newCourseACL2 = from s in db.ACL
                                   join t in db.CourseInfo on s.ObjectID equals t.CourseID
                                   where s.ObjectType.Trim() == "course" && s.SubjectType.Trim() == "user" && s.ACLValue / 16 < 1
                                   select new { ObjectName = t.CourseName, SubjectName = s.SubjectName, ObjectType = "课程", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "弱", AclID = s.AclID };
                var newACL = newClassACL.Concat(newCourseACL.Concat(newClassACL2.Concat(newCourseACL2)));
                this.Store2.DataSource = newACL;
                this.Store2.DataBind();
            }
        }

        //protected string Power(int i, int j)
        //{
        //    if (i / 10 == 1)
        //    {
        //        return "强";
        //    }
        //    else if (j / 10 == 1)
        //    {
        //        return "弱";
        //    }
        //    else
        //        return "无";
        //}

        protected void RoleDataBind()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newClassACL = from s in db.ACL
                                  join t in db.ClassInfo on s.ObjectID equals t.ClassID
                                  where s.ObjectType.Trim() == "class" && s.SubjectType.Trim() == "role" && s.ACLValue / 16 >= 1
                                  select new { ObjectName = t.ClassName, SubjectName = s.SubjectName, ObjectType = "班级", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "强", AclID = s.AclID };
                var newClassACL2 = from s in db.ACL
                                   join t in db.ClassInfo on s.ObjectID equals t.ClassID
                                   where s.ObjectType.Trim() == "class" && s.SubjectType.Trim() == "role" && s.ACLValue / 16 < 1
                                   select new { ObjectName = t.ClassName, SubjectName = s.SubjectName, ObjectType = "班级", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "弱", AclID = s.AclID };
                var newCourseACL = from s in db.ACL
                                   join t in db.CourseInfo on s.ObjectID equals t.CourseID
                                   where s.ObjectType.Trim() == "course" && s.SubjectType.Trim() == "role" && s.ACLValue / 16 >= 1
                                   select new { ObjectName = t.CourseName, SubjectName = s.SubjectName, ObjectType = "课程", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "强", AclID = s.AclID };
                var newCourseACL2 = from s in db.ACL
                                    join t in db.CourseInfo on s.ObjectID equals t.CourseID
                                    where s.ObjectType.Trim() == "course" && s.SubjectType.Trim() == "role" && s.ACLValue / 16 < 1
                                    select new { ObjectName = t.CourseName, SubjectName = s.SubjectName, ObjectType = "课程", ACLCSValue = Convert.ToInt32(s.ACLValue / 10000000), ACLCWValue = Convert.ToInt32(s.AclID / 1000), ACLRSValue = Convert.ToInt32(s.ACLValue / 1000000), ACLRWValue = Convert.ToInt32(s.AclID / 100), ACLUSValue = Convert.ToInt32(s.ACLValue / 100000), ACLUWValue = Convert.ToInt32(s.AclID / 10), ACLDSValue = Convert.ToInt32(s.ACLValue / 10000), ACLDWValue = Convert.ToInt32(s.AclID), Power = "弱", AclID = s.AclID };
                var newACL = newClassACL.Concat(newCourseACL.Concat(newClassACL2.Concat(newCourseACL2)));
                this.Store6.DataSource = newACL;
                this.Store6.DataBind();
            }
        }

        protected void MenuDataBind()
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newMenuACL = from m in db.ACL
                                 where m.ObjectType.Trim() == "menu" && m.SubjectType.Trim() == "role"
                                 select new { ObjectName = Menu(Convert.ToInt32(m.ObjectID)), SubjectName = m.SubjectName, ObjectType = "目录", Power = "有", AclID = m.AclID };
                Store9.DataSource = newMenuACL;
                Store9.DataBind();
            }
             }

        protected string Menu(int i)
        {
            switch(i)
            {
                case 1: return "系统首页";
                case 2: return "成绩查询";
                case 3: return "数据录入";
                case 4: return "数据维护";
                case 5: return "成绩分析";
                case 6: return "帐号管理";
                case 7: return "系统管理";
                case 8: return "云桌面";
                case 9: return "系统帮助";
                case 10: return "隐藏边栏";
                default: return "";
            }
                 
        }

        protected string Check(bool c)
        {
            if (c)
                return "1";
            else
                return "0";
        }

        protected void Button1_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            string SubjectType = "user";
            string SubjectName = ComboBox1.Value.ToString();
            int ObjectID = Convert.ToInt32(ComboBox2.SelectedItem.Value);
            string ObjectType = "course";
            string num = Check(Checkbox1.Checked) + Check(Checkbox2.Checked) + Check(Checkbox3.Checked) + Check(Checkbox4.Checked);
            if (num != "0000")
            {
                if (Radio1.Checked)
                {
                    num += "0000";
                }
                else
                {
                    num = "0000" + num;
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newObject = from s in db.ClassInfo
                                    where s.ClassID == ObjectID && s.ClassName == ComboBox2.Text.Trim()
                                    select s;
                    if (newObject.Count() != 0)
                    {
                        ObjectType = "class";
                    }
                    ExamAnalysis.Model.ACL newacl = new ExamAnalysis.Model.ACL();
                    newacl.ACLValue = Convert.ToInt32(num,2);
                    newacl.ObjectID = ObjectID;
                    newacl.ObjectType = ObjectType;
                    newacl.SubjectName = SubjectName;
                    newacl.SubjectType = SubjectType;
                    db.ACL.InsertOnSubmit(newacl);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加")).Show();
                    UserDataBind();
                }
            }
            else
            {
                X.Msg.Alert("提示信息", String.Format("请选择相关操作")).Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            int aclid=0;
            RowSelectionModel sm = this.ACLInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                   aclid  = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newaclid = from s in db.ACL
                                   where s.AclID == aclid
                                   select s;
                    ExamAnalysis.Model.ACL acl = newaclid.First();
                    db.ACL.DeleteOnSubmit(acl);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    UserDataBind();
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个项").Show();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            string SubjectType = "role";
            string SubjectName = ComboBox3.Value.ToString();
            int ObjectID = Convert.ToInt32(ComboBox4.SelectedItem.Value);
            string ObjectType = "course";
            string num = Check(Checkbox5.Checked) + Check(Checkbox6.Checked) + Check(Checkbox7.Checked) + Check(Checkbox8.Checked);
            if (num != "0000")
            {
                if (Radio3.Checked)
                {
                    num += "0000";
                }
                else
                {
                    num = "0000" + num;
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newObject = from s in db.ClassInfo
                                    where s.ClassID == ObjectID && s.ClassName == ComboBox4.Text.Trim()
                                    select s;
                    if (newObject.Count() != 0)
                    {
                        ObjectType = "class";
                    }
                    ExamAnalysis.Model.ACL newacl = new ExamAnalysis.Model.ACL();
                    newacl.ACLValue = Convert.ToInt32(num,2);
                    newacl.ObjectID = ObjectID;
                    newacl.ObjectType = ObjectType;
                    newacl.SubjectName = SubjectName;
                    newacl.SubjectType = SubjectType;
                    db.ACL.InsertOnSubmit(newacl);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加")).Show();
                    RoleDataBind();
                }
            }
            else
            {
                X.Msg.Alert("提示信息", String.Format("请选择相关操作")).Show();
            }
        }

        protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            int aclid = 0;
            RowSelectionModel sm = this.ACLInfoGridPanel2.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    aclid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newaclid = from s in db.ACL
                                   where s.AclID == aclid
                                   select s;
                    ExamAnalysis.Model.ACL acl = newaclid.First();
                    db.ACL.DeleteOnSubmit(acl);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    RoleDataBind();
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个项").Show();
            }
        }

        protected void Button6_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            string SubjectType = "role";
            string SubjectName = ComboBox5.Value.ToString();
            if (SubjectName != "")
            {
                string ObjectType = "menu";
                for (int i = 0; i < CheckboxGroup2.CheckedItems.Count(); i++)
                {
                    int ObjectID = Convert.ToInt32(CheckboxGroup2.CheckedItems[i].Name);
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        var newMenuACL = from m in db.ACL
                                         where m.ObjectType.Trim() == "menu" && m.SubjectType.Trim() == "role" && m.ObjectID == ObjectID && m.SubjectName == SubjectName
                                         select m;
                        if (newMenuACL.Count() == 0)
                        {
                            ExamAnalysis.Model.ACL newacl = new ExamAnalysis.Model.ACL();
                            newacl.ACLValue = 255;
                            newacl.ObjectID = ObjectID;
                            newacl.ObjectType = ObjectType;
                            newacl.SubjectName = SubjectName;
                            newacl.SubjectType = SubjectType;
                            db.ACL.InsertOnSubmit(newacl);
                            db.SubmitChanges();
                            X.Msg.Alert("提示信息", String.Format("成功添加")).Show();
                        }
                        MenuDataBind();
                    }
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个项").Show();
            }
        }

        protected void Button7_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            int aclid = 0;
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    aclid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newaclid = from s in db.ACL
                                   where s.AclID == aclid
                                   select s;
                    ExamAnalysis.Model.ACL acl = newaclid.First();
                    db.ACL.DeleteOnSubmit(acl);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    MenuDataBind();
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个项").Show();
            }
        }
    }
}